package cn.adalab.hellojava;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HelloWorld extends JFrame implements ActionListener {

    public static final String MESSAGE_BEFORE_CLICK = "hElLo WoRlD!";
    public static final String MESSAGE_AFTER_CLICK = "Hello World!";

    JButton btn;
    JLabel label;

    public HelloWorld(String title) {
        super(title);
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn = new JButton("点我");
        btn.setActionCommand("click-me");
        btn.addActionListener(this);
        add(btn, BorderLayout.SOUTH);

        label = new JLabel(MESSAGE_BEFORE_CLICK);
        label.setFont(new Font("Serif", Font.PLAIN, 40));
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        add(label, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getActionCommand().equals("click-me")) {
            label.setText(MESSAGE_AFTER_CLICK);
        }
    }

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld("Hello World");
        helloWorld.setVisible(true);
    }
}